package controller.link.battle;

import view.client.Console;
import model.Hearer;
import view.client.PanelBattle;
import java.io.Serializable;
import java.util.ArrayList;
import model.game.Item;
import controller.Link;
import model.game.Player;
import model.game.Skill;
import model.User;
import model.Attendant;
import model.Server;

public class Ask4BattleInfo implements Link, Serializable {

    private Player me;
    private Player opponent;
    private String move;

    public Ask4BattleInfo() {
    }

    public Ask4BattleInfo(String move) {
        this.move = move;
    }

    public Ask4BattleInfo(Hearer h) {
        h.send((Link) this);
    }

    @Override
    public void serverUse(Server server, Attendant at) {
        User user = at.getUser();
        this.me = user.getPlayer();
        this.opponent = server.getBattle(at).getOpponPlayer(user.getPlayer());
        at.send((Link) this);
    }

    @Override
    public void clientUse(Console console) {
        ArrayList<Skill> skills = console.getSkills();
        ArrayList<Item> items = console.getItems();
        PanelBattle pb = console.getPb();
        console.getTxtArea().append(move + "\n");
        skills = me.getSkills();
        items = me.getItems();
        console.setSkills(me.getSkills());
        console.setItems(me.getItems());
        pb.getPbLife().setStringPainted(true);
        pb.getPbLife().setMaximum(me.getTotalLife());
        pb.getPbLife().setStringPainted(true);
        pb.getPbLife().setValue(me.getLife());
        pb.getPbLife().setString(Integer.toString(me.getLife()) + "/" + Integer.toString(me.getTotalLife()));
        pb.getPbLife().repaint();
        pb.getPbEnergy().setStringPainted(true);
        pb.getPbEnergy().setMaximum(me.getTotalEnergy());
        pb.getPbEnergy().setStringPainted(true);
        pb.getPbEnergy().setValue(me.getEnergy());
        pb.getPbEnergy().setString(Integer.toString(me.getEnergy()) + "/" + Integer.toString(me.getTotalEnergy()));
        pb.getPbEnergy().repaint();
        pb.getPbEnemyLife().setStringPainted(true);
        pb.getPbEnemyLife().setMaximum(opponent.getTotalLife());
        pb.getPbEnemyLife().setStringPainted(true);
        pb.getPbEnemyLife().setValue(opponent.getLife());
        pb.getPbEnemyLife().setString(Integer.toString(opponent.getLife()) + "/" + Integer.toString(opponent.getTotalLife()));
        pb.getPbEnemyLife().repaint();
        pb.getPbEnemyEnergy().setStringPainted(true);
        pb.getPbEnemyEnergy().setMaximum(opponent.getTotalEnergy());
        pb.getPbEnemyEnergy().setStringPainted(true);
        pb.getPbEnemyEnergy().setValue(opponent.getEnergy());
        pb.getPbEnemyEnergy().setString(Integer.toString(opponent.getEnergy()) + "/" + Integer.toString(opponent.getTotalEnergy()));
        pb.getPbEnemyEnergy().repaint();
        pb.getModelSkill().removeAllElements();
        pb.getModelConsumable().removeAllElements();
        for (int i = 0; i < skills.size(); i++) {
            pb.getModelSkill().add(i, skills.get(i).getName());
        }
        for (int i = 0; i < items.size(); i++) {
            pb.getModelConsumable().add(i, items.get(i).getName());
        }
        pb.revalidate();
    }
}
